package netudp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class DatagramReceive {
	public static void main(String[] args) throws IOException {
		try {
			DatagramSocket ds=new DatagramSocket(9988);//建立端口
			byte[] buf=new byte[1024];//建立缓冲区
			DatagramPacket dp= new DatagramPacket(buf, 1024);//建立一个包
			ds.receive(dp);//接受数据（等待发送）
			String s=new  String(dp.getData(), 0, dp.getLength());//获取数据
			System.out.println("***"+s);
			//dp.setData("我很好".getBytes());//回发
			
			DatagramPacket outp=new DatagramPacket(
					buf, 1024,dp.getAddress(),dp.getPort());
			outp.setData("你很好".getBytes());
			
			ds.send(outp);
		
			ds.close();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
